View Javadoc

1   /*
2    * jGuild Project: jRPM
3    * Released under the Apache License ( http://www.apache.org/LICENSE )
4    */
5   package com.jguild.jrpm.io;
6   
7   import java.io.DataInputStream;
8   import java.io.IOException;
9   
10  import org.apache.log4j.Logger;
11  
12  import com.jguild.jrpm.io.constant.RPMIndexType;
13  
14  /***
15   * RPM Index Entry (16 byte).
16   * 
17   * <ol>
18   * <li>tag; what the data is for (4 bytes)</li>
19   * <li>type; what type of data is stored (see RPMFileType.java) (4 bytes)
20   * </li>
21   * <li>offset; where to the data begins (4 bytes)</li>
22   * <li>count; how much data of the type is there (4 bytes)</li>
23   * </ol>
24   * 
25   * @version $Id: IndexEntry.java,v 1.5 2004/05/06 20:59:22 mkuss Exp $
26   */
27  public class IndexEntry {
28  
29      private static final int INDEX_LENGTH = 16;
30  
31      private static final Logger logger = Logger.getLogger(IndexEntry.class);
32  
33      private RPMIndexType type;
34  
35      private long count;
36  
37      private long offset;
38  
39      private long tag;
40  
41      /***
42       * Constructs a IndexEntry out of a InputStream. This will read the tag id,
43       * the data type, the offset of the data and the number of elements of
44       * data. Note that the number of elements varies in its meaning depending
45       * on the data type.
46       * 
47       * @param inputStream
48       *           An InputStream containing a rpm file.
49       * 
50       * @throws IOException
51       *            If an error occurs during reading from stream
52       */
53      public IndexEntry(DataInputStream inputStream) throws IOException {
54          tag = inputStream.readInt();
55  
56          if (logger.isDebugEnabled()) {
57              logger.debug("tag:" + tag);
58          }
59  
60          type = RPMIndexType.getRPMIndexType(inputStream.readInt());
61  
62          if (logger.isDebugEnabled()) {
63              logger.debug("type: " + type);
64          }
65  
66          offset = inputStream.readInt();
67  
68          if (logger.isDebugEnabled()) {
69              logger.debug("offset: " + offset);
70          }
71  
72          count = inputStream.readInt();
73  
74          if (logger.isDebugEnabled()) {
75              logger.debug("count: " + count);
76          }
77  
78          check(!type.equals(RPMIndexType.UNKNOWN));
79      }
80  
81      /***
82       * Get the number of elements of this data type
83       * 
84       * @return The number of elements
85       */
86      public long getCount() {
87          return count;
88      }
89  
90      /***
91       * Returns the offset of this data type
92       * 
93       * @return The offset
94       */
95      public long getOffset() {
96          return offset;
97      }
98  
99      /***
100      * Get the size of this index entry in bytes
101      * 
102      * @return The size
103      */
104     public int getSize() {
105         return INDEX_LENGTH;
106     }
107 
108     /***
109      * Get the tag id as a long
110      * 
111      * @return The tag id
112      */
113     public long getTag() {
114         return tag;
115     }
116 
117     /***
118      * Get the data type of this entry
119      * 
120      * @return The data type
121      */
122     public RPMIndexType getType() {
123         return type;
124     }
125 
126     /***
127      * Asserts a boolean value and throws an exception if it is false
128      * 
129      * @param test
130      *           A boolean test variable
131      * 
132      * @throws IOException
133      *            if the variable test is false
134      */
135     private static final void check(boolean test) throws IOException {
136         if (!test) { throw new IOException("Corrupted archive"); }
137     }
138 }